# -*- coding: UTF-8 -*-
# @Time : 2022-02-05 3:11
# @User：My.
# @File : ExcelData.py
# 获取测试用例文档，初始化sheet对象
# 定义获取sheet 方法
# 获取有效的测试用例
# 根据TC_ID获取响应的列表数据
# 判断是否运行，Y执行测试用例，N
# 获取运行测试用例列表
from Utils.ExcelUtil import ExcelReader
from Utils.LogUtil import my_log
from TestData.ExcelConfig import ExcelSheet, TestCases, CaseData, TestSteps, Elements


def get_by_tc_id(data_list, tc_id):
    """
    根据tc_id来获取数据，获取相应的列表数据的封装方法
    :param data_list:
    :param tc_id:
    :return:
    """
    data_all_tc = []
    for data in data_list:
        if data[CaseData.DATA_TC_ID] == tc_id:
            data_all_tc.append(data)
    return data_all_tc


class Data:
    # 获取测试用例文档，初始化sheet对象
    def __init__(self, case_file):
        # 添加日志
        self.log = my_log('ExcelData')
        self.reader_cases = ExcelReader(case_file, ExcelSheet.test_case)
        self.reader_data = ExcelReader(case_file, ExcelSheet.test_data)
        self.reader_steps = ExcelReader(case_file, ExcelSheet.test_step)
        self.reader_elements = ExcelReader(case_file, ExcelSheet.test_ele)

    # 定义获取sheet 方法
    def get_cases_sheet(self):
        """
        获取测试用例TestCases的sheet的数据
        :return:
        """
        return self.reader_cases.data()

    def get_data_sheet(self):
        """
        获取测试用例CaseData的sheet的数据
        :return:
        """
        return self.reader_data.data()

    def get_step_sheet(self):
        """
        获取测试用例TestSteps的sheet的数据
        :return:
        """
        return self.reader_steps.data()

    def get_element_sheet(self):
        """
        获取测试用例Elements的sheet的数据
        :return:
        """
        return self.reader_elements.data()

    # 获取cases有效的测试用例
    def get_cases_all(self):
        """
        获取全部测试用例
        :return:
        """
        data_list = self.get_cases_sheet()
        res_cases = []
        for data in data_list:
            if data[TestCases.CASES_TC_ID] != '':
                res_cases.append(data)
        return res_cases

    # 获取data有效的测试用例
    def get_data_all(self):
        """
        获取全部测试用例
        :return:
        """
        data_list = self.get_data_sheet()
        res_data = []
        for data in data_list:
            if data[CaseData.DATA_TC_ID] != '':
                res_data.append(data)
        return res_data

    # 获取step有效的测试用例
    def get_step_all(self):
        """
        获取全部测试用例
        :return:
        """
        data_list = self.get_step_sheet()
        res_step = []
        for data in data_list:
            if data[TestSteps.STEP_TC_ID] != '':
                res_step.append(data)
        return res_step

    # 获取element有效的测试用例
    def get_element_all(self):
        """
        获取全部测试用例
        :return:
        """
        data_list = self.get_element_sheet()
        res_element = []
        for data in data_list:
            if data[Elements.ELE_TC_ID] != '':
                res_element.append(data)
        return res_element

    # 根据TC_ID获取CaseData相应的列表数据
    def get_data_by_tc_id(self, tc_id):
        """
        根据TC_ID获取CaseData相应的列表数据
        :param tc_id:
        :return:
        """
        data_all = self.get_data_all()
        # data_all_tc_id = []
        # for data in data_all:
        # if data['TC_ID'] == tc_id:
        # data_all_tc_id.append(data)
        data_all_tc_id = get_by_tc_id(data_all, tc_id)
        return data_all_tc_id

    # 根据TC_ID获取TestSteps相应的列表数据
    def get_step_by_tc_id(self, tc_id):
        """
        根据TC_ID获取TestSteps相应的列表数据
        :param tc_id:
        :return:
        """
        step_all = self.get_step_all()
        step_all_tc_id = get_by_tc_id(step_all, tc_id)
        return step_all_tc_id

    # 根据TC_ID获取Elements相应的列表数据
    def get_element_by_tc_id(self, tc_id):
        """
        根据TC_ID获取Elements相应的列表数据
        :param tc_id:
        :return:
        """
        element_all = self.get_element_all()
        element_all_tc_id = get_by_tc_id(element_all, tc_id)
        return element_all_tc_id

    def get_elements_by_element(self, tc_id, element_name):
        """
        根据步骤TestSteps中的元素名称和tc_id获取Elements相应的数据
        :param tc_id:
        :param element_name:
        :return:
        """
        elements = self.get_element_by_tc_id(tc_id)
        res = None
        for ele in elements:
            if str(ele[Elements.ELE_NAME]) == str(element_name):
                res = ele
        return res

    # 判断是否运行，Y执行测试用例，N
    def get_run_cases(self):
        # 获取全部测试用例
        run_list = self.get_cases_all()
        run_cases_list = []
        for line in run_list:
            if str(line[TestCases.CASE_IS_RUN]).lower() == 'y':
                run_cases_list.append(line)
        return run_cases_list

    def get_run_data(self, tc_id):
        # 获取data全部测试用例
        run_list = self.get_data_all()
        run_data_list = []
        for line in run_list:
            if str(line[CaseData.DATA_IS_RUN]).lower() == 'y' and tc_id in line[CaseData.DATA_TC_ID]:
                run_data_list.append(line)
        return run_data_list

    # 获取运行测试用例列表
    def run_list(self):
        # 获取testcase执行测试用例列表
        """
        获取testcase执行测试用例列表
        :return:
        """
        cases = self.get_run_cases()
        self.log.debug("获取TestCases表测试用例个数:{0}, 数据内容:{1}".format(len(cases), cases))
        # 根据这个列表中的TC_ID来获取对应的CaseData表的数据
        data_list = list()
        for case in cases:
            tc_id = case[TestCases.CASES_TC_ID]
            case_data_list = self.get_run_data(tc_id)
            # 备注
            note = case[TestCases.CASE_NOTE]
            # 描述
            desc = case[TestCases.CASES_DESC]
            for data in case_data_list:
                data.update({TestCases.CASE_NOTE: note})
                data.update({TestCases.CASES_DESC: desc})
            data_list.extend(case_data_list)
        self.log.debug("获取CaseData表测试用例个数:{0}, 数据内容:{1}".format(len(data_list), data_list))
        return data_list


if __name__ == '__main__':
    data = Data("../TestData/TestData.xlsx")
    # print(data.get_cases_all())
    # print(data.get_data_all())
    # print(data.get_step_all())
    # print(data.get_element_all())
    # print(data.get_data_by_tc_id('TC_Login'))
    # print(data.get_step_by_tc_id('TC_Login'))
    # print(data.get_element_by_tc_id('TC_Login'))
    # print(data.get_run_cases())
    # print(data.get_run_data('TC_Login'))
    # print(data.run_list())
    # print(data.get_element_by_tc_id('TC_Login'))
    # print(data.get_elements_by_element('TC_Login', 'EL_login_QQ'))

    print(data.run_list())


